package com.qf.comfig;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author zhaojian
 */
@Component
public class PublisherConfirmAndReturnConfig implements RabbitTemplate.ConfirmCallback ,RabbitTemplate.ReturnCallback{

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //在创建时，执行init方法
    @PostConstruct
    public void init(){
        //指定 ConfirmCallback 设置confirm机制
        rabbitTemplate.setConfirmCallback(this);

        //指定 ReturnCallback 设置return机制
        rabbitTemplate.setReturnCallback(this);
    }

    //保证消息到达交换机
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        //id是消息的唯一标识
//        correlationData.getId();
        if(ack){
            System.out.println("消息已经送达到Exchange");
        }else{
            System.out.println("消息没有送达到Exchange");
        }
    }

    //保证消息从交换机转发到队列
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("消息未到达队列");
    }
}